package com.zhangxiaofanfan.hotcode.second;

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/unique-paths/description/?envType=study-plan-v2&envId=top-100-liked">62. 不同路径</a>
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode091 {
    public static void main(String[] args) {
        HotCode091 hotCode = new HotCode091();
        System.out.println(hotCode.uniquePaths(7, 3));
    }

    public int uniquePaths(int m, int n) {
        if (m < 1 || n < 1) {
            throw new RuntimeException("矩阵长和宽不满足要求");
        }
        int[][] matrix = new int[m][n];
        Arrays.fill(matrix[0], 1);
        Arrays.stream(matrix).forEach(row -> row[0] = 1);
        for (int rowIndex = 1, colIndex; rowIndex < m; rowIndex++) {
            for (colIndex = 1; colIndex < n; colIndex++) {
                matrix[rowIndex][colIndex] = matrix[rowIndex - 1][colIndex] + matrix[rowIndex][colIndex - 1];
            }
        }
        return matrix[m - 1][n - 1];
    }
}